#include <graphics.h>
#include <conio.h>
#include <math.h>

#define width 500
#define high 400
#define PI 3.14159

int main()
{
	initgraph(width,high);
	
	int center_x,center_y;//中心点
	center_x=width/2;
	center_y=high/2;

	int secondLength;//秒针长度
	int minlen;//分针长度
	int hourlen;//时针长度

	secondLength=width/4;
	minlen=width/5;
	hourlen=width/7;

	int secondEnd_x,secondEnd_y;	//秒针终点坐标
	int minEnd_x,minEnd_y;			//分针终点坐标
	int hourEnd_x,hourEnd_y;		//时针终点坐标

	double secondAngle=0;	//秒针转过角度
	double minAngle=0;		//分针转过的角度
	double hourAngle=0;		//时针转过的角度

	SYSTEMTIME ti;//定义变量存储系统时间
	
	BeginBatchDraw();

	while(1)
	{
		//画钟
		setlinestyle(PS_SOLID,3);
		setcolor(WHITE);
		circle(center_x,center_y,width/3);//画钟的轮廓

		DWORD a[4] = {1,5*PI};		//画每秒的点
		setlinestyle(PS_USERSTYLE | PS_ENDCAP_ROUND, 1, a, 4);
		circle(center_x,center_y,width*3/10);
		
		setfillcolor(YELLOW);			//画3,6,9,12时的点
		fillcircle((width/2-width*3/10),high/2,3);
		fillcircle((width/2+width*3/10),high/2,3);
		fillcircle(width/2,(high/2+width*3/10),3);
		fillcircle(width/2,(high/2-width*3/10),3);

		outtextxy(center_x-25,center_y+width/6,"我的时钟");//显示文字

		GetLocalTime(&ti);

		secondAngle=ti.wSecond*2*PI/60;		//一圈共2*PI，一圈60秒，一秒钟走2*PI/60
		minAngle=(ti.wMinute+secondAngle/60)*2*PI/60;
		hourAngle=(ti.wHour+minAngle/12)*2*PI/12;
		//秒针位置
		secondEnd_x=center_x+secondLength*cos(secondAngle-PI/2);
		secondEnd_y=center_y+secondLength*sin(secondAngle-PI/2);
		//分针位置
		minEnd_x=center_x+minlen*cos(minAngle-PI/2);
		minEnd_y=center_y+minlen*sin(minAngle-PI/2);
		//时针位置
		hourEnd_x=center_x+hourlen*cos(hourAngle-PI/2);
		hourEnd_y=center_y+hourlen*sin(hourAngle-PI/2);

		//画秒针
		setlinestyle(PS_SOLID,2);
		setcolor(WHITE);
		line(center_x,center_y,secondEnd_x,secondEnd_y);
		//画分针
		setlinestyle(PS_SOLID,4);
		setcolor(BLUE);
		line(center_x,center_y,minEnd_x,minEnd_y);
		//画时针
		setlinestyle(PS_SOLID,6);
		setcolor(RED);
		line(center_x,center_y,hourEnd_x,hourEnd_y);

		FlushBatchDraw();

		Sleep(100);

		//隐藏前面一帧的秒针，分针，时针
		setcolor(BLACK);
		line(center_x,center_y,secondEnd_x,secondEnd_y);
		line(center_x,center_y,minEnd_x,minEnd_y);
		line(center_x,center_y,hourEnd_x,hourEnd_y);
	}

	EndBatchDraw();
	getch();
	closegraph();
	return 0;
}